import Vue from 'vue'



Vue.filter('json', (obj) => {
	return JSON.stringify(obj)
})
Vue.filter("toUpCase", (data) => {

	if (data) {
		return data.toUpperCase()
	} else {
		return ""
	}

})


function add0(m) {
	return m < 10 ? '0' + m : m
}

Vue.filter("sliceName", (data) => {
	if (data) {
		return data.slice(0, 1) + "(先生/女士)"
	} else {
		return "--"
	}
})




Vue.filter("sliceMobile", (data) => {
	if (data) {
		return data.slice(0, 3) + "****" + data.slice(7, 11)
	} else {
		return "--"
	}
})
Vue.filter("sliceDeviceId", (data) => {
	if (data) {
		return  "****" + data.slice(data.length - 5, data.length - 1)
	} else {
		return "--"
	}
})



Vue.filter("sliceMobile_4", (data) => {
	if (data) {
		return data.slice(7, 11)
	} else {
		return "--"
	}
})

Vue.filter("sliceFileName", (data) => {
	if (data.length > 30) {
		return data.slice(0, 15) + "......" + data.slice(data.length - 7, data.length)
	} else {
		return data
	}
})

Vue.filter("sliceFileName_20", (data) => {
	if (data.length > 20) {
		return data.slice(0, 10) + "......" + data.slice(data.length - 7, data.length)
	} else {
		return data
	}
})

Vue.filter("sliceFileName_5", (data) => {
	if (!!data) {
		if (data.length > 11) {
			return data.slice(0, 5) + "......" + data.slice(data.length - 6, data.length)
		} else {
			return data
		}
	} else {
		return "--"
	}
})



Vue.filter("time", (timestamp) => {
	//timestamp是整数，否则要parseInt转换,不会出现少个0的情况
	var time = new Date(timestamp);
	var year = time.getFullYear();
	var month = add0(parseInt(time.getMonth()) + 1)
	var date = add0(parseInt(time.getDate()))
	var hours = add0(parseInt(time.getHours()))
	var minutes = add0(parseInt(time.getMinutes()))
	var seconds = add0(parseInt(time.getSeconds()))
	if (timestamp) {
		return year + '-' + month + '-' + date + ' ' + hours + ':' + minutes + ':' + seconds;
	} else {
		return "--"
	}

})

Vue.filter("time_", (timestamp) => {
	//timestamp是整数，否则要parseInt转换,不会出现少个0的情况
	var time = new Date(timestamp);
	var year = time.getFullYear();
	var month = add0(parseInt(time.getMonth()) + 1)
	var date = add0(parseInt(time.getDate()))
	var hours = add0(parseInt(time.getHours()))
	var minutes = add0(parseInt(time.getMinutes()))
	var seconds = add0(parseInt(time.getSeconds()))
	return year + '-' + month + '-' + date
})

Vue.filter("time_text", (timestamp) => {
	//timestamp是整数，否则要parseInt转换,不会出现少个0的情况	
	var time = new Date(timestamp);
	var year = time.getFullYear();
	var month = add0(parseInt(time.getMonth()) + 1)
	var date = add0(parseInt(time.getDate()))
	var hours = add0(parseInt(time.getHours()))
	var minutes = add0(parseInt(time.getMinutes()))
	var seconds = add0(parseInt(time.getSeconds()))
	return year + '-' + month + '-' + date + ' ' + hours + ':' + minutes + ":" + seconds;
})
Vue.filter("time_", (timestamp) => {
	//timestamp是整数，否则要parseInt转换,不会出现少个0的情况
	var time = new Date(timestamp);
	var year = time.getFullYear();
	var month = add0(parseInt(time.getMonth()) + 1)
	var date = add0(parseInt(time.getDate()))
	var hours = add0(parseInt(time.getHours()))
	var minutes = add0(parseInt(time.getMinutes()))
	var seconds = add0(parseInt(time.getSeconds()))
	return year + '-' + month + '-' + date
})

Vue.filter("time_day", (timestamp) => {
	//timestamp是整数，否则要parseInt转换,不会出现少个0的情况

	var time = new Date(timestamp);
	var month = parseInt(time.getMonth()) + 1
	var date = parseInt(time.getDate())
	return timestamp ? month + '月' + date + "日" : "- 月 - 日"
})
Vue.filter("time_seconds", (timestamp, type) => {
	// 换算成倒计时 时分秒
	let time = parseInt(timestamp)
	let s = "0"
	let m = "00"
	let h = "00"

	h = parseInt(time / 3600) < 10 ? "0" + parseInt(time / 3600).toString() : parseInt(time / 3600)

	m = parseInt(time % 3600 / 60) < 10 ? ("0" + parseInt(time % 3600 / 60)).toString() : parseInt(time %
		3600 /
		60)

	s = parseInt(time % 60) < 10 ? "0" + parseInt(time % 60).toString() : parseInt(time % 60)

	switch (type) {
		case "all":
			return h + ":" + m + ":" + s
			break;
		case "h":
			return h
			break;
		case "m":
			return m
			break;
		case "s":
			return s
			break;

	}



})

Vue.filter("time_ago", (timestamp) => {
	// 换算成倒计时 时分秒

	let time = parseInt(new Date(timestamp).getTime() / 1000)
	let nowTime = parseInt(new Date().getTime() / 1000)
	let diffValue = nowTime - time
	// 5分钟
	if (diffValue < 5 * 60) {
		return "刚刚"
	}
	// 1小时
	if (diffValue > 60 * 60 && diffValue < 12 * 60 * 60) {
		return "1小时以前"
	}

	// 12小时
	if (diffValue > 12 * 60 * 60 && diffValue < 24 * 60 * 60) {
		return "12小时以前"
	}
	// 一天
	if (diffValue > 24 * 60 * 60 && diffValue < 3 * 24 * 60 * 60) {
		return "1天以前"
	}
	if (diffValue > 3 * 24 * 60 * 60 && diffValue < 1 * 24 * 60 * 60) {
		return "3天以前"
	}
	if (diffValue > 30 * 24 * 60 * 60) {
		return "一个月以前"
	}
})







Vue.filter("to_F6", (val) => {
	let data = parseFloat(val)
	if (data == 0) {
		return 0
	} else {
		return data.toFixed(6)
	}
})


Vue.filter("to_F6", (val) => {
	let data = parseFloat(val)
	if (data == 0) {
		return 0
	} else {
		return data.toFixed(6)
	}
})

//判断字符串长度
Vue.filter("strLength", (str) => {
	var len = 0;
	for (var i = 0; i < str.length; i++) {
		var c = str.charCodeAt(i);
		//单字节加1
		if ((c >= 0x0001 && c <= 0x007e) || (0xff60 <= c && c <= 0xff9f)) {
			len++;
		} else {
			//汉字加2
			len += 2;
		}
	}
	return Math.floor(len / 2);
})

//判断字符串长度
Vue.filter("getFileSize", (bytes) => {
	const units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
	let size = bytes;
	let unitIndex = 0;

	while (size >= 1024 && unitIndex < units.length - 1) {
		size /= 1024;

		unitIndex++;
	}

	// 保留两位小数，四舍五入
	size = Math.round(size * 100) / 100;
	if (bytes) {
		return `${size} ${units[unitIndex]}`;
	} else {
		return "--"
	}
})

//获取文件类型
Vue.filter("getFileType", (data) => {
	if (data) {
		let arr = data.split("/")
		return arr[arr.length - 1].toUpperCase()
	}

})
//.换砖为 ：
Vue.filter("replaceChat", (data) => {
	if (data.indexOf(".") == -1) {
		return data
	} else {
		return data.replace(/\./g, ':');
	}


})
//秒转为时间倒计时
Vue.filter("sToM", (data , type) => {
	// 换算成倒计时 时分秒
	let time = parseInt(data)
	let s = "0"
	let m = "00"
	let h = "00"
	h = parseInt(time / 3600) < 10 ? "0" + parseInt(time / 3600).toString() : parseInt(time / 3600)
	m = parseInt(time % 3600 / 60) < 10 ? ("0" + parseInt(time % 3600 / 60)).toString() : parseInt(time %3600 /60)

	s = parseInt(time % 60) < 10 ? "0" + parseInt(time % 60).toString() : parseInt(time % 60)
	if (data) {
		switch (type) {
			case "all":
				return h + ":" + m + ":" + s
				break;
			case "h":
				return h
				break;
			case "m":
				return m
				break;
			case "s":
				return s
				break;
		}
	} else {
		return "0：0"
	}




})